from flask import Flask,request,session
import config
from flask_sqlalchemy import SQLAlchemy
from jmgdxcx.extensions import db,migrate,bootstrap
import os
import urllib3
#
import secrets
from datetime import timedelta

from jmgdxcx.blueprints.xcx import xcx_bp

basedir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)



def create_app(config_name=None):
    if config_name is None:
        config_name = os.getenv('FLASK_CONFIG', 'development')

    app = Flask('jmgdpyxcx')
    app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
    app.config['SECRET_KEY'] = '123456'
    
    app.config['WTF_CSRF_ENABLED'] = False  # 启用 CSRF 保护
    #app.config['WTF_CSRF_SECRET_KEY'] = '1DGA43TYJ56'  # CSRF 令牌的密钥
    #app.config['WTF_CSRF_TIME_LIMIT'] = 3600

    #
    app.secret_key = secrets.token_hex(16)
    app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=7)

##########
    app.config.from_object(config)

    register_extensions(app)
    register_blueprints(app)

    return app



def register_extensions(app):
    db.init_app(app)
    migrate.init_app(app, db)
    bootstrap.init_app(app)

def register_blueprints(app):
    app.register_blueprint(xcx_bp,url_prefix='/xcx')
    #app.register_blueprint(jmd_bp,url_prefix='/jmd')
    #app.register_blueprint(reqdata_bp, url_prefix='/reqdata')